import os
from pillow_heif import open_heif
from PIL import Image
# 输入和输出目录
input_dir = "D:\\workspace\\colmap\\home_ws\\images_raw"
output_dir = "D:\\workspace\\colmap\\home_ws\\images"

# 确保输出目录存在
os.makedirs(output_dir, exist_ok=True)

# 遍历所有 HEIC 文件
for filename in os.listdir(input_dir):
    if filename.lower().endswith(".heic"):
        heif_file = open_heif(os.path.join(input_dir, filename))
        img = Image.frombytes(heif_file.mode, heif_file.size, bytes(heif_file.data), "raw", heif_file.mode)
        # heif_image = pyheif.read(os.path.join(input_dir, filename))
        # img = Image.frombytes(
        #     heif_image.mode, 
        #     heif_image.size, 
        #     bytes(heif_image.data)  # 这里转换为 bytes
        # )
        
        # 保存为 JPG
        jpg_filename = os.path.splitext(filename)[0] + ".jpg"
        img.save(os.path.join(output_dir, jpg_filename), "JPEG")

print("批量转换完成！")
